package com.xueqiu.movieplaymanagersystem.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import com.xueqiu.movieplaymanagersystem.entity.BlogTag;
import com.xueqiu.movieplaymanagersystem.entity.Tag;
import com.xueqiu.movieplaymanagersystem.mapper.BlogTagMapper;
import com.xueqiu.movieplaymanagersystem.service.BlogTagService;
import com.xueqiu.movieplaymanagersystem.service.PublicService;
import com.xueqiu.movieplaymanagersystem.utils.UUIDUtils;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xueqiu
 */
@Service
public class BlogTagServiceImpl implements BlogTagService {
    private PublicService<Tag> tagService;
    @Autowired
    private BlogTagMapper blogTagMapper;

    @Autowired
    public void setTagService(
            @Qualifier("tagService")
            PublicService<Tag> tagService
    ) {
        this.tagService = tagService;
    }

    /**
     * 批量新增
     *
     * @param tagIdList 数据源
     * @param blogId    新闻ID
     * @return 新增是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveBatch(List<String> tagIdList, String blogId) {
        //先判断标签id列表是否合法
        if (!isTagListSafe(tagIdList)) {
            return -1;
        }

        //批量新增；优化了访问数据库的速度
        List<BlogTag> blogTagList = tagIdList.stream()
                .map(tagId -> new BlogTag(UUIDUtils.uuid(), tagId, blogId))
                .collect(Collectors.toList());
        return blogTagMapper.insertBatch(blogTagList);
    }

    /**
     * 工具方法；判断标签id列表所有元素是否全部合法
     *
     * @param tagIdList 标签id列表
     * @return true，合法
     */
    private boolean isTagListSafe(List<String> tagIdList) {
        //通过特定参数，造成全数据查询
        List<Tag> originList = tagService.findByKeyWord(null, -1, -1).getList();
        //筛选id，然后判断筛选后的id列表是否 “全包含” 参数tagIds中的元素
        return originList
                .stream()
                .map(Tag::getId)
                .collect(Collectors.toSet())
                .containsAll(tagIdList);
    }

    /**
     * 编辑
     *
     * @param tagIdList 数据源
     * @param blogId    新闻ID
     * @return 编辑条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editBatch(List<String> tagIdList, String blogId) {
        //判断当“不是清除全部标签而是更改标签”的操作时；再判断标签id列表是否合法
        if (!tagIdList.isEmpty() && !isTagListSafe(tagIdList)) {
            return -1;
        }

        //查找属于该blog的标签列表originList
        List<BlogTag> originList = blogTagMapper.selectByBlogId(blogId);

        //如果tagIds参数为空，而originList不为空，说明用户执行的是清除全部标签的操作
        if (tagIdList.isEmpty() && !CollectionUtils.isEmpty(originList)) {
            return removeByBlogId(blogId);
        //如果tagIds参数为空且originList为空，说明标签没有必要修改
        } else if (tagIdList.isEmpty() && CollectionUtils.isEmpty(originList)) {
            return 1;
        }

        //如果tagIds参数不为空，而originList也不为空，就代表“不是清除全部标签而是更改标签”
        if (!CollectionUtils.isEmpty(originList)) {
            List<String> originTagIdList = originList.stream()
                    .map(BlogTag::getTagId)
                    .collect(Collectors.toList());
            //originTagIdList参数和originList内容相同，说明没有更新，
            if (tagIdList.size() == originList.size() && new HashSet<>(originTagIdList).containsAll(tagIdList)) {
                return 1;
            }
            //如果内容不同，则先将旧的标签删除
            int delete = removeByBlogId(blogId);
            //如果旧标签删除成功，则插入新的标签
            if (delete >= 1) {
                return saveBatch(tagIdList, blogId);
            } else {
                //否则删除执行失败；返回错误
                return -1;
            }
        }

        //如果tagIds参数不为空而originList为空，说明tagIds更新了，且数据库中不存在任何标签，直接插入即可
        return saveBatch(tagIdList, blogId);
    }

    /**
     * 删除
     *
     * @param blogId 新闻ID
     * @return 删除成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByBlogId(String blogId) {
        return blogTagMapper.deleteByBlogId(blogId);
    }

    /**
     * 删除
     *
     * @param tagId 标签ID
     * @return 删除成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByTagId(String tagId) {
        return blogTagMapper.deleteByTagId(tagId);
    }

    /**
     * 批量删除
     *
     * @param tagIdList id字段列表
     * @return 删除条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByTagIdBatch(List<String> tagIdList) {
        return blogTagMapper.deleteByTagIdBatch(tagIdList);
    }
}
